//右方个人信息模块
define('userInfo', ['config', 'cookie', 'jquery', 'handlebars'], function(config, cookie, $, Handlebars){
	//定义hanlebars helper
	function helper(){
		//判断头像是否为空
		Handlebars.registerHelper('if_empty', function(value, options){
			if(value == ''){
				return options.fn(this);
			}
			else{
				return options.inverse(this);
			}
		});
	}


	//调整头像高度（超过宽度时截去）
	function adjustAvatar(rowClassName){
		var colWidth = $(rowClassName + ':eq(0) img').width();
		$(rowClassName).css('height', colWidth);

		//更改浏览器窗口大小时
		$(window).resize(function(){
			var colWidth = $(rowClassName + ':eq(0) img').width();
			$(rowClassName).css('height', colWidth);
		});
	}
	

	//返回函数
	return {
		hasAvatar: function(){
			var userId = cookie.getCookie('userId');
			if (userId != ''){
				var user_request = $.ajax({
					url: config['api']['getUserInfo'][0].replace('<user_id>', userId),
					type: config['api']['getUserInfo'][1],

					dataType: 'json',
					async: true,
					error: function(err){
						console.log('get userInfo failed!');
					},
					success: function(data){
						console.log(data);
						helper();

						var source = $("#info-template").html();
						template = Handlebars.compile(source);
						$('.user-info').append(template(data['user']));

						adjustAvatar('.image-info');
					}
				});
			}
		},
		listInfo: function(){
			var userId = cookie.getCookie('userId');
			if (userId != ''){
				var user_request = $.ajax({
					url: config['api']['getUserInfo'][0].replace('<user_id>', userId),
					type: config['api']['getUserInfo'][1],

					dataType: 'json',
					async: true,
					error: function(err){
						console.log('get userInfo failed!');
					},
					success: function(data){
						console.log(data);
						helper();

						var source = $("#sideMenu").html();
						template = Handlebars.compile(source);
						$('.side_container').append(template(data['user']));
					}
				});
			}
			else{
				$('.side_menu').css('display', 'none');
			}
		}
	};
});
